package cn.jinjimi.app.main.widget.ToolBar;

import android.content.Context;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import cn.jinjimi.app.R;


/**
 * Toolbar helper
 * <p/>
 *
 * @author Jinsen
 * @date 16/1/8
 */
public class ToolbarHelper {

    private Context mContext;

    private FrameLayout mContentView;
    private View mUserView;
    private ImageView mBottomLine;

    private Toolbar mToolbar;
    private int mToolbarHeight = 0;

    public ToolbarHelper(Context context, int layoutResId) {
        this.mContext = context;
        mToolbarHeight = mContext.getResources().getDimensionPixelSize(R.dimen.title_bar_height);
        buildContentView(layoutResId);
        addTitleBottomLine();
    }

    private void buildContentView(int layoutResId) {
        mContentView = new FrameLayout(mContext);
        mContentView.setFitsSystemWindows(true);
        mContentView.setBackgroundResource(R.color.common_bg);
        ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.MATCH_PARENT);
        mContentView.setLayoutParams(params);

        mUserView = LayoutInflater.from(mContext).inflate(layoutResId, null);
        FrameLayout.LayoutParams paramsBiz = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT,
                FrameLayout.LayoutParams.MATCH_PARENT);
        paramsBiz.topMargin = mToolbarHeight;
        mContentView.addView(mUserView, paramsBiz);
    }

    private void addTitleBottomLine() {
        mBottomLine = new ImageView(mContext);
        mBottomLine.setBackgroundResource(R.drawable.shade_top_bar);
        FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT,1);
        params.topMargin = mToolbarHeight;
        mContentView.addView(mBottomLine, params);
    }

    public CommonToolbar createToolbar() {
        mToolbar = new CommonToolbar(mContext);
        FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT,
                mToolbarHeight);
        mContentView.addView(mToolbar, params);
        return (CommonToolbar) mToolbar;
    }

    public WebToolbar createWebToolbar() {
        mToolbar = new WebToolbar(mContext);
        FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT,
                mToolbarHeight);
        mContentView.addView(mToolbar, params);

        return (WebToolbar) mToolbar;
    }

    public View getContentVew() {
        return mContentView;
    }

    public Toolbar getToolbar() {
        return mToolbar;
    }

    public final void resetTitleTop(int top) {
        addStatusBarBg(top);
        ((FrameLayout.LayoutParams) mToolbar.getLayoutParams()).topMargin = top;
        FrameLayout.LayoutParams bottomlineLp = (FrameLayout.LayoutParams) mBottomLine.getLayoutParams();
        bottomlineLp.topMargin = bottomlineLp.topMargin + top;
        FrameLayout.LayoutParams userViewLp = (FrameLayout.LayoutParams) mUserView.getLayoutParams();
        userViewLp.topMargin = userViewLp.topMargin + top;
    }

    private void addStatusBarBg(int height) {
        ImageView mBottomLine = new ImageView(mContext);
        mBottomLine.setBackgroundResource(R.color.black);
        FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT,
                height);
        mContentView.addView(mBottomLine, 0, params);
    }

    public void hideToolBar(){
       FrameLayout.LayoutParams lp = (FrameLayout.LayoutParams) mContentView.getLayoutParams();
        lp.topMargin = -mToolbarHeight-mContext.getResources().getDimensionPixelSize(R.dimen.dp_2);
        mContentView.setLayoutParams(lp);
    }

    public void showToolBar(){
        FrameLayout.LayoutParams lp = (FrameLayout.LayoutParams) mContentView.getLayoutParams();
        lp.topMargin = 0;
        mContentView.setLayoutParams(lp);
    }

}